import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
  // 可通过 computed: mapState(['forcedLogin']), 这种方式挂载到各个页面,进行调用
  state: {
    /**
         * 是否需要强制登录
         */
    forcedLogin: false,
    hasLogin: false,
    userName: '',
    userId: '',
    token: ''
  },
  mutations: {
    // 可通过 ...mapMutations(['login']), 挂载到各个页面,从而通过 this.login 调用本方法
    login: function (state, user) {
      console.log('state=' + JSON.stringify(state) + ',userName=' + JSON.stringify(user))
      state.hasLogin = true
      state.token = user.token
      state.userName = user.user.username || '新用户'
      state.userId = user.user.id
    },
    logout: function (state) {
      state.userName = ''
      state.hasLogin = false
      state.token = ''
    }
  }
})
export default store
